import {
  selectExportOSList,
  selectExportOSDetail,
  selectExportOSInfo,
  operateExportOS,
  selectFileList,
  delExportOS,
  cancelOrder,
  operateOrderFile,
  getOrderFile,
  getAutoOrderNo,
  getAreaByAddress,
  contactList,
  operateAddress,
  delContact,
  discernAddress,
  getTruckTypeList,
} from '@/services/myOrder/exportOS';
import { getTracking } from '@/services/myOrder/trail';

import prompt from '@/components/Prompt';
import { getPageSize } from '@/utils/common';

export default {
  namespace: 'exportOSSearch',

  state: {
    exportOSList: {},
    exportOSDetail: {},
    formValues: {},
    cargoInfoList: {}, //货物
    trackData: [],
    orderNoValues: {},
    addressList: {},
  },

  effects: {
    // 一、查询订单管理列表
    *selectExportOSList({ payload, callback }, { call, put }) {
      const response = yield call(selectExportOSList, payload);
      const {
        code,
        // data: { items, pageCount, totalCount, pageIndex },
      } = response;
      const data = response.data || {};
      let pagination = {
        list: data.items || [],
        pageSize: getPageSize(),
        total: data.totalCount || 0,
        pages: data.pageCount || 1,
        currentPage: data.pageIndex,
      };
      const { pages, pageSize, currentPage, total, list } = pagination;
      yield put({
        type: 'saveAllValus',
        payload: {
          exportOSList: {
            pagination: {
              current: currentPage,
              pageSize,
              total,
              pages,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    /**
     * 查询订单管理详情
     */
    *selectExportOSDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectExportOSDetail, payload);
      const { code, data } = response;
      let newData = [];
      const cargoInfoData = { list: data.orderPartList || [] };
      if (code === 0) {
        // newData = data;
        // const filePayLoad = { bizId: payload.id, fileBizType: 'PAPERS' };
        // const fileResponse = yield call(selectFileList, filePayLoad);
        // if (fileResponse.code === 0) {
        //   newData.fileList = fileResponse.data;
        // } else {
        //   newData.fileList = [];
        // }
      }
      yield put({
        type: 'detail',
        payload: {
          exportOSDetail: { [payload.id]: data },
        },
      });
      yield put({
        type: 'detail',
        payload: {
          cargoInfoList: { [payload.id]: cargoInfoData },
        },
      });
      callback && callback(data);
    },
    /**
     * 新增编辑基本信息
     */
    *operateExportOS({ payload, callback }, { call }) {
      const response = yield call(operateExportOS, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    //删除订单管理：
    *delExportOS({ payload, callback }, { call }) {
      const response = yield call(delExportOS, payload);
      const { code, data, message } = response;
      const content = `删除${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback();
    },
    //取消订单管理：
    *cancelOrder({ payload, callback }, { call }) {
      const response = yield call(cancelOrder, payload);
      const { code, data, message } = response;
      const content = `取消${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback();
    },

    //附件补录：
    *operateOrderFile({ payload, callback }, { call }) {
      const response = yield call(operateOrderFile, payload);
      const { code, data, message } = response;
      const content = `附件补录${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback();
    },
    *getOrderFile({ payload, callback }, { call }) {
      const response = yield call(getOrderFile, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      callback && callback(data);
    },
    //自动获取单号：
    *getAutoOrderNo({ payload, callback }, { call }) {
      const response = yield call(getAutoOrderNo, payload);
      const { code, data } = response;
      if (code !== 0) return;
      callback && callback(data);
    },
    //自动获取城市：
    *getAreaByAddress({ payload, callback }, { call }) {
      const response = yield call(getAreaByAddress, payload);
      const { code, data } = response;
      if (code !== 0) return;
      callback && callback(data);
    },
    //二、轨迹
    *getTracking({ payload, callback }, { call, put }) {
      // const truckDataTest = {
      //   code: 0,
      //   message: '请求(或处理)成功',
      //   data: {
      //     list: [
      //       {
      //         querySource: 'HIC',
      //         orderNo: payload.orderNo,
      //         orderSource: 'OCS',
      //         bizStatus: 'SUBMIT',
      //         customerCode: '204570',
      //         customerName: 'Lenovo (Hong Kong) Limited',
      //         orderTime: '2019-09-13 07:59:44',
      //         orderTracking: [
      //           // { eventTime: '2020-04-24 16:47:00', eventDesc: 'Signed confirmed' },
      //           { eventTime: '2020-04-23 17:25:00', eventDesc: 'Ship out' },
      //           { eventTime: '2020-04-23 16:00:00', eventDesc: 'Picked up' },
      //           { eventTime: '2020-04-22 13:02:52', eventDesc: 'Ready to ship' },
      //           { eventTime: '2020-04-21 19:20:36', eventDesc: 'Packing completed' },
      //           { eventTime: '2020-04-21 17:23:13', eventDesc: 'Picking completed' },
      //           { eventTime: '2020-04-21 19:20:36', eventDesc: 'Order created' },
      //         ],
      //       },
      //     ],
      //   },
      // };
      // const { data, code } = truckDataTest;
      
        const response = yield call(getTracking, payload);
        const { data, code } = response;
        yield put({
          type: 'saveAllValus',
          payload: {
            trackData: data,
          },
        });
        callback && callback(data); //测试
    },

    //三、地址
    *contactList({ payload, callback }, { call, put }) {
      const response = yield call(contactList, payload);
      const {
        code,
        // data: { items, pageCount, totalCount, pageIndex },
      } = response;
      const data = response.data || {};
      let pagination = {
        list: data.items || [],
        pageSize: getPageSize(),
        total: data.totalCount || 0,
        pages: data.pageCount || 1,
        currentPage: data.pageIndex,
      };
      const { pages, pageSize, currentPage, total, list } = pagination;
      yield put({
        type: 'saveAllValus',
        payload: {
          addressList: {
            pagination: {
              current: currentPage,
              pageSize,
              total,
              pages,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    /**
     * 新增编辑地址
     */
    *operateAddress({ payload, callback }, { call }) {
      const response = yield call(operateAddress, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    //删除地址：
    *delContact({ payload, callback }, { call }) {
      const response = yield call(delContact, payload);
      const { code, data, message } = response;
      const content = `删除${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback();
    },
    //地址识别
    *discernAddress({ payload, callback }, { call }) {
      const response = yield call(discernAddress, payload);
      const { code, data, message } = response;
      const content = `识别${message}，请检查拆分地址信息是否准确`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(data);
    },
    //获取车型
    *getTruckTypeList({ payload, callback }, { call }) {
      const response = yield call(getTruckTypeList, payload);
      const { code, data, message } = response;
      if (code !== 0) return;
      callback && callback(data);
    },
    //

    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },
  },

  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
    list(state, { payload }) {
      // let param = '';
      // for (let k in payload) {
      //   param = k;
      // }
      return {
        ...state,
        [param]: { ...payload[param] },
      };
    },
  },
};
